/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
	if (!name) return;
	if (typeof content !== 'string') {
		content = JSON.stringify(content);
	}
	window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getStore = name => {
	if (!name) return;
	return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
	if (!name) return;
	window.localStorage.removeItem(name);
}

/**
 * 获取style样式
 */
export const getStyle = (element, attr, NumberMode = 'int') => {
	let target;
	// scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
	if (attr === 'scrollTop') {
		target = element.scrollTop;
	} else if (element.currentStyle) {
		target = element.currentStyle[attr];
	} else {
		target = document.defaultView.getComputedStyle(element, null)[attr];
	}
	//在获取 opactiy 时需要获取小数 parseFloat
	return NumberMode == 'float' ? parseFloat(target) : parseInt(target);
}

/**
 * 页面到达底部，加载更多
 */
export const loadMore = (element, callback) => {
	let windowHeight = window.screen.height;
	let height;
	let setTop;
	let paddingBottom;
	let marginBottom;
	let requestFram;
	let oldScrollTop;

	document.body.addEventListener('scroll', () => {
		loadMore();
	}, false)
	//运动开始时获取元素 高度 和 offseTop, pading, margin
	element.addEventListener('touchstart', () => {
		height = element.offsetHeight;
		setTop = element.offsetTop;
		paddingBottom = getStyle(element, 'paddingBottom');
		marginBottom = getStyle(element, 'marginBottom');
	}, {
		passive: true
	})

	//运动过程中保持监听 scrollTop 的值判断是否到达底部
	element.addEventListener('touchmove', () => {
		loadMore();
	}, {
		passive: true
	})

	//运动结束时判断是否有惯性运动，惯性运动结束判断是非到达底部
	element.addEventListener('touchend', () => {
		oldScrollTop = document.body.scrollTop;
		moveEnd();
	}, {
		passive: true
	})

	const moveEnd = () => {
		requestFram = requestAnimationFrame(() => {
			if (document.body.scrollTop != oldScrollTop) {
				oldScrollTop = document.body.scrollTop;
				loadMore();
				moveEnd();
			} else {
				cancelAnimationFrame(requestFram);
				//为了防止鼠标抬起时已经渲染好数据从而导致重获取数据，应该重新获取dom高度
				height = element.offsetHeight;
				loadMore();
			}
		})
	}

	const loadMore = () => {
		if (document.body.scrollTop + windowHeight >= height + setTop + paddingBottom + marginBottom) {
			callback();
		}
	}
}

export const formatDate = secs =>{
	var t = new Date(secs)
	var year = t.getFullYear()
	var month = t.getMonth() + 1
	if (month < 10) {
		month = '0' + month
	}
	var date = t.getDate()
	if (date < 10) {
		date = '0' + date
	}
	var hour = t.getHours()
	if (hour < 10) {
		hour = '0' + hour
	}
	var minute = t.getMinutes()
	if (minute < 10) {
		minute = '0' + minute
	}
	var second = t.getSeconds()
	if (second < 10) {
		second = '0' + second
	}
	return year + '-' + month + '-' + date
}
// 年月日时分
export const formatDateMin = secs =>{
	var t = new Date(secs)
	var year = t.getFullYear()
	var month = t.getMonth() + 1
	if (month < 10) {
		month = '0' + month
	}
	var date = t.getDate()
	if (date < 10) {
		date = '0' + date
	}
	var hour = t.getHours()
	if (hour < 10) {
		hour = '0' + hour
	}
	var minute = t.getMinutes()
	if (minute < 10) {
		minute = '0' + minute
	}
	var second = t.getSeconds()
	if (second < 10) {
		second = '0' + second
	}
	return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
}

//验证手机号
export const checkMobile = str =>{
	var res = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
	if (res.test(str)) {
		return str;
	} else {
		return false;
	}
}
//验证身份证
export const checkID = str => {
	var res = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	if (res.test(str)) {
		return str;
	} else {
		return false;
	}
}
